package org.ricks.conditon;

import org.ricks.ioc.event.IEvent;

import java.util.function.Consumer;

// 抽象任务条件
public abstract class TaskCondition<E extends IEvent> implements Condition {
    protected final int targetValue;
    protected volatile int currentValue;
    private final Consumer<TaskCondition<E>> doneCallback;
    private final Class<E> eventType;
    private volatile boolean isConditionMet = false;

    protected TaskCondition(int targetValue, Consumer<TaskCondition<E>> doneCallback, Class<E> eventType) {
        this.targetValue = targetValue;
        this.doneCallback = doneCallback;
        this.eventType = eventType;
    }

    @Override
    public void emit(IEvent event) {
        if (!isConditionMet && eventType.isInstance(event)) {
            handleEvent(eventType.cast(event));
        }
    }

    protected abstract void handleEvent(E event);

    @Override
    public boolean isDone() {
        if (isConditionMet) return true;
        boolean done = currentValue >= targetValue;
        if (done) {
            isConditionMet = true;
            if (doneCallback != null) doneCallback.accept(this);
        }
        return done;
    }

    // 可选：提供进度访问方法
    public int getCurrentValue() { return currentValue; }
    public int getTargetValue() { return targetValue; }
}
